//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System.ComponentModel;
using NopSolutions.NopCommerce.BusinessLogic.Model;
using System.Web.UI.WebControls;
using NopSolutions.NopCommerce.Controls;

namespace NopSolutions.NopCommerce.Web.Modules
{
    public partial class AddressDisplay : BaseNopFrontendUserControl
    {
        private bool _showEditButton = true;
        private bool _showDeleteButton = true;
        private bool _showRadioButton = false;

        public Address Address {
            set {
                Address address = value;
                if (address != null) {
                    hfAddressId.Value = address.AddressId.ToString();

                    lnkEditAddress.NavigateUrl = string.Format("~/addressedit.aspx?customerguid={0}&addressid={1}", address.Customer.CustomerGuid, address.AddressId);
                    lnkDeleteAddress.NavigateUrl = string.Format("~/addressedit.aspx?customerguid={0}&addressid={1}&delete={2}", address.Customer.CustomerGuid, address.AddressId, true);

                    // legal entity
                    if (address.CompanyLegalForm) {
                        pnlIndividualLabel.Visible = false;
                        pnlCompanyLabel.Visible = pnlCompanyInfo.Visible = true;

                        litCompany.Text = Server.HtmlEncode(address.Company);
                        litCompanyFiscalCode.Text = Server.HtmlEncode(address.CompanyFiscalCode);
                        litCompanyTradeRegisterNumber.Text = Server.HtmlEncode(address.CompanyRegComNumber);
                        litCompanyBank.Text = Server.HtmlEncode(address.CompanyBank);
                        litCompanyBankAccount.Text = Server.HtmlEncode(address.CompanyBankAccount);
                    }
                    else {
                        // individual
                        pnlIndividualLabel.Visible = true;
                        pnlCompanyLabel.Visible = pnlCompanyInfo.Visible = false;

                        lFullName.Text = Server.HtmlEncode(string.Format("{0} {1}", address.FirstName, address.LastName));
                        lEmail.Text = Server.HtmlEncode(address.Email);
                    }

                    lPhoneNumber.Text = Server.HtmlEncode(address.PhoneNumber);

                    if (string.IsNullOrWhiteSpace(address.MobilePhoneNumber))
                        pnlMobilePhone.Visible = false;
                    lMobilePhoneNumber.Text = Server.HtmlEncode(address.MobilePhoneNumber);

                    if (string.IsNullOrWhiteSpace(address.FaxNumber))
                        pnlFax.Visible = false;
                    lFaxNumber.Text = Server.HtmlEncode(address.FaxNumber);

                    this.lAddress1.Text = Server.HtmlEncode(address.FullAddress);

                    var country = address.Country;
                    if (country != null)
                        this.lCountry.Text = Server.HtmlEncode(country.Name);
                    var stateProvince = address.StateProvince;
                    if (stateProvince != null)
                        this.lStateProvince.Text = Server.HtmlEncode(stateProvince.Name);
                    this.lCity.Text = Server.HtmlEncode(address.City);
                    this.lZipPostalCode.Text = Server.HtmlEncode(address.ZipPostalCode);
                }
            }
        }

        public int AddressId {
            get {
                var hfAddressId = FindControl("hfAddressId") as HiddenField;
                if (hfAddressId != null) {
                    return int.Parse(hfAddressId.Value);
                }

                return -1;
            }
        }

        public bool IsSelected {
            get {
                var rbAddress = FindControl("rbAddress") as GlobalRadioButton;
                if (rbAddress != null && rbAddress.Checked) {
                    return true;
                }
                return false;
            }
        }

        [DefaultValue(false)]
        public bool ShowRadioButton {
            get { return _showRadioButton; }
            set { _showRadioButton = value; }
        }

        [DefaultValue(true)]
        public bool ShowDeleteButton {
            get { return _showDeleteButton; }
            set { _showDeleteButton = value; }
        }

        [DefaultValue(true)]
        public bool ShowEditButton {
            get { return _showEditButton; }
            set { _showEditButton = value; }
        }
    }
}